package com.jamal2362.apktool.util;

import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class SwipeGestureDetector extends SimpleOnGestureListener {
	private static final int SWIPE_MIN_DISTANCE = 50;
	private static final int SWIPE_MAX_OFF_PATH = 200;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private SwipeGestureDetector.SwipeListener listener;

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		try {
			float diffAbs = Math.abs(e1.getY() - e2.getY());
			float diff = e1.getX() - e2.getX();

			if (diffAbs > SWIPE_MAX_OFF_PATH)
				return false;

			// Left swipe
			if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				listener.onLeftSwipe();
			} else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				listener.onRightSwipe();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public interface SwipeListener
	{
		public void onLeftSwipe();
		public void onRightSwipe();
	}

	public void setSwipeListener(SwipeListener listener)
	{
		this.listener = listener;
	}
}
